//-----------------------------------------------------------------------
// <copyright file="FeedbackRatingPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections;
    using System.Text;
    using Simnova.Collaborate.View;

    public class FeedbackRatingPresenter
    {
        private IFeedbackRating feedbackRatingView;
        private int feedbackId;
        private int userId;

        public FeedbackRatingPresenter(IFeedbackRating feedbackRatingView, int feedbackId, int userId)
        {
            this.feedbackRatingView = feedbackRatingView;
            this.feedbackId = feedbackId;
            this.userId = userId;
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.Feedback feedback = collaborateClient.GetFeedback(this.feedbackId);
            collaborateClient.Close();
            this.feedbackRatingView.Title = feedback.Title;
            this.feedbackRatingView.Description = feedback.Description;
            this.LoadRating();
            this.LoadRatings();
        }

        public void Save()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.FeedbackRating feedbackRating = collaborateClient.GetFeedbackRatingByFeedback(this.userId, this.feedbackId);
            if (feedbackRating == null)
            {
                feedbackRating = new AppServer.FeedbackRating();
                feedbackRating.UserId = this.userId;
                feedbackRating.FeedbackId = this.feedbackId;
            }

            feedbackRating.Rating = this.feedbackRatingView.Rating;
            feedbackRating.Comment = this.feedbackRatingView.Comment;
            collaborateClient.SaveFeedbackRating(feedbackRating);
            collaborateClient.Close();
            this.LoadRating();
            this.LoadRatings();
        }

        private void LoadRating()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.FeedbackRating feedbackRating = collaborateClient.GetFeedbackRatingByFeedback(this.userId, this.feedbackId);
            collaborateClient.Close();
            if (feedbackRating != null)
            {
                this.feedbackRatingView.Rating = feedbackRating.Rating;
                this.feedbackRatingView.Comment = feedbackRating.Comment;
            }
        }

        private void LoadRatings()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            IList ratings = collaborateClient.GetFeedbackRatings(this.feedbackId);
            collaborateClient.Close();
            if (ratings != null)
            {
                this.feedbackRatingView.Ratings = ratings;
            }
        }
    }
}
